package com.wzh.house.aspect;

import com.alibaba.fastjson.JSONObject;
import com.wzh.house.dao.UserLogDao;
import com.wzh.house.entity.UserLogEntity;
import com.wzh.house.vo.SysUserVo;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @author WZH
 * @title: AdminAop
 * @projectName find_house
 * @description:
 * @date 2022/3/24
 */
@Component
@Aspect
public class AdminAopAspect {

    @Autowired
    private UserLogDao userLogDao;

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    @Pointcut("execution(* com.wzh.house.service.impl.NewHouseServiceImpl.updateStatus(..))")
    public void updateStatusPointCut() {

    }

    @After("updateStatusPointCut()")
    public void updateStatus(JoinPoint point) {
        UserLogEntity userLog = new UserLogEntity();
        userLog.setCreateDate(new Date());
        String userJsonString = redisTemplate.opsForValue().get("user");
        SysUserVo sysUserVo = JSONObject.parseObject(userJsonString, SysUserVo.class);
        userLog.setUsername(sysUserVo.getUsername());
        userLog.setMethod(point.getSignature().getDeclaringTypeName()+'.'+point.getSignature().getName());
        userLog.setParams(JSONObject.toJSONString(point.getArgs()));
        userLog.setOperation(sysUserVo.getUsername()+"审核房源状态为"+point.getArgs()[1]);
        userLogDao.insert(userLog);
    }
}
